import javax.microedition.lcdui.*;
import java.util.Vector;

//#if !(SCREENGROUP_128x128xSpecial || SCREENGROUP_128x160xSpecial)
class MenuSaver {
    public byte menuId;
    public int menuOption;

    public MenuSaver(byte menuId, int menuOption) {
        this.menuId = menuId;
        this.menuOption = menuOption;
    }
}
//#endif

public final class GameMenu {
    // constants
    static public final int MENU_RET_CMD_NONE = -1;

    // screen types
    static public final byte MENU_SCR_NONE                  = 0;
    static public final byte MENU_SCR_HELP                  = 1;
    static public final byte MENU_SCR_SELECT_LEV            = 2;
    static public final byte MENU_SCR_EDIT_NAME             = 3;
    static public final byte MENU_SCR_MESSAGE_BOX           = 4;
    static public final byte MENU_SCR_HIGH_SCORE            = 5;
    static public final byte MENU_SCR_FINAL_SCENE           = 6;
    //#if USE_SOUND_VOLUME_CONTROL
        static public final byte MENU_SCR_VOLUME_CONTROL    = 7;
    //#endif
    //#if USE_GAME_TIPS
        static public final byte MENU_SCR_GAME_TIPS         = 8;
    //#endif

    // menu types
    static public final byte MENU_ID_EDIT_NAME      = 0;
    static public final byte MENU_ID_MAIN           = 1;
    static public final byte MENU_ID_HIGH_SCORE     = 2;
    static public final byte MENU_ID_PAUSE          = 3;
    static public final byte MENU_ID_SELECT_LEVEL   = 4;
    static public final byte MENU_ID_SETTINGS       = 5;
    static public final byte MENU_ID_INFO           = 6;
    static public final byte MENU_ID_HELP           = 7;
    static public final byte MENU_ID_VIEW_CTRLS     = 8;
    static public final byte MENU_ID_ABOUT          = 9;
    static public final byte MENU_ID_QUESTION       = 10;  // Yes, No
    static public final byte MENU_ID_INFORMATION    = 11;  // Ok
    static public final byte MENU_ID_FINAL_SCENE    = 12;
    //#if USE_SOUND_VOLUME_CONTROL
        static public final byte MENU_ID_VOLUME_CONTROL = 13;
    //#endif
    //#if USE_GAME_TIPS
        static public final byte MENU_ID_GAME_TIPS      = 14;
    //#endif

    //#if (SCREENGROUP_128x127xSpecial || SCREENGROUP_128x128xSpecial || SCREENGROUP_128x160xSpecial || SCREENGROUP_240x260xLimited)
        static public final byte SPLASH_TITLE_LAYER_INDEX     = 0;
        static public final byte SPLASH_COPYRIGHT_MARV_INDEX  = 1;
        static public final byte SPLASH_BG_LAYER_INDEX        = 0;
        static public final byte SPLASH_MARV_LAYER_INDEX      = 0;
    //#else
        static public final byte SPLASH_BG_LAYER_INDEX        = 0;
        static public final byte SPLASH_TITLE_LAYER_INDEX     = 1;
        static public final byte SPLASH_MARV_LAYER_INDEX      = 2;
        static public final byte SPLASH_COPYRIGHT_MARV_INDEX  = 3;
    //#endif

    // menus
    private static int nMainMenu[] =    {
                                            StringsID.nStrID_NewGame, 
                                            StringsID.nStrID_SelectLevel, 
                                            StringsID.nStrID_HighScores,
                                            StringsID.nStrID_Settings, 
                                            StringsID.nStrID_Help, 
                                            StringsID.nStrID_About
                                        };

    private static int nSettingsMenu[] =    {
                                                //#if SOUND_NOKIA_RT || SOUND_MMAPI || SAMSUNG_MMF || SHARP_MMF
                                                    StringsID.nStrID_SndOn,
                                                //#endif
                                                //#if USE_SOUND_VOLUME_CONTROL
                                                    StringsID.nStrID_Volume,
                                                //#endif
                                                //#if USE_GAME_TIPS
                                                    StringsID.nStrID_GameTipsOn,
                                                //#endif
                                                StringsID.nStrID_EditName
                                            };

    private static int nPauseMenu[] =   {
                                            //#if SOUND_NOKIA_RT || SOUND_MMAPI || SAMSUNG_MMF || SHARP_MMF
                                                StringsID.nStrID_SndOn,
                                            //#endif
                                            //#if USE_GAME_TIPS
                                                StringsID.nStrID_GameTipsOn,
                                            //#endif
                                            StringsID.nStrID_QuitLevel, 
                                            StringsID.nStrID_QuitGame
                                        };

    private static int nInfoMenu[] =    {
                                            StringsID.nStrID_HowToPlay, 
                                            StringsID.nStrID_Controls
                                        };

    // menu size
    private static boolean blNeedRecalcSize = true;
    private static int iMenuWidth = 0;
    private static int iMenuHeight = 0;

    // menus history
    private static Vector vMenuStack = new Vector();
    private static byte curMenuId;
   
    private static int nCurMenu[];
    public static byte iMenuScreenID;
    private static int iMenuOption = 0;
    private static boolean blNeedRedraw = true;
    public static boolean blReady = false;

    private static Animation animSplash = new Animation(AnimID.AnimID_Splash);
    private static Image imgSplash = null;
    private static Image imgMarv = null;
    private static Image imgTitle = null;
    private static Image imgCopyright = null;

    //#if PAUSE_MENU_BG_RASTER
        public static Image imgRastr = null;
    //#endif

    private static int nCounter = 0;

    // variables for showing multyline text
    private static Vector vText = new Vector();
    private static int textSize = 0;
    private static int nMaxWndStr = 0;
    private static int nCurLine = 0;
    public static int nSelLine = 0;

    // additional menu items (menu bar)
    private static int mLeftBtn;
    private static int mRightBtn;

    //#if FULL_CANVAS
        //#if IMGGROUP_12x12 || IMGGROUP_12x12xLimited
            private static final int MENU_BAR_HEIGHT = 17;
        //#elif IMGGROUP_18x18
            private static final int MENU_BAR_HEIGHT = 25;
        //#elif IMGGROUP_24x24 || IMGGROUP_24x24xLimited
            private static final int MENU_BAR_HEIGHT = 33;
        //#elif IMGGROUP_36x36
            private static final int MENU_BAR_HEIGHT = 50;
        //#endif
    //#else
        private static final int MENU_BAR_HEIGHT = 0;
    //#endif

    // constants for "Editor"
    public final static byte EDITOR_MAX_LENGTH = 10;
    public final static short EDITOR_DELAY = 750;
    //#if Nokia && E61
        public final static char NUM_TO_LETTER[] =  { 
                                                        'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 
                                                        'a', 's', 'd', 'f', 'g', 'h', 'i', 'k', 'l', 
                                                        'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.'
                                                    };
    //#else
        public final static char NUM_TO_LETTER[][] =    { 
                                                            { '.', '-' },
                                                            { 'a', 'b', 'c' },
                                                            { 'd', 'e', 'f' },
                                                            { 'g', 'h', 'i' },
                                                            { 'j', 'k', 'l' },
                                                            { 'm', 'n', 'o' },
                                                            { 'p', 'q', 'r', 's' },
                                                            { 't', 'u', 'v' },
                                                            { 'w', 'x', 'y', 'z' }
                                                        };
    //#endif
    /* table-version
    public final static String TABLE_OF_LETTER[][] =    { 
                                                            {" ", "a", "b", "c", "d", "e", "f", "g", "h", "i"},
                                                            {"j", "k", "l", "m", "n", "o", "p", "q", "r", "s"},
                                                            {"t", "u", "v", "w", "x", "y", "z", "0", "1", "2"},
                                                            {"3", "4", "5", "6", "7", "8", "9", "!", "\"", "@"},
                                                            {"'", "$", "-", "+", ",", ".", "/", ":", ";", "?"}
                                                        };
    */
    // variable for "Editor"
    public static int editorLength = 0;
    public static StringBuffer editStr = new StringBuffer();
    private static int lastKeyNum;
    private static int lastLetter;
    private static long lastTime;
    /* table-version
    private static int editorPosX = 0;
    private static int editorPosY = 0;
    */

    // variable for message screen
    public static byte bTilesMatrix[][];
    //#if !IMGGROUP_12x12xLimited
        public static LevObj decorObjects[];
    //#endif
    public static short messPosY;

    //#if USE_FINAL_SPLASH
        // final scene
        private static Image imgFinalScene = null;
    //#endif


    //#if !FULL_CANVAS
        private static MTMCanvas canvas;
    //#endif

    /**
     *
     */
    //#if SOUND_NOKIA_RT || SOUND_MMAPI || SAMSUNG_MMF || SHARP_MMF
    public static boolean isSoundOn() {
        return (nSettingsMenu[0] == StringsID.nStrID_SndOn);
    }

    public static void enableSound(boolean on) {
        if (on) {
            nPauseMenu[0] = nSettingsMenu[0] = StringsID.nStrID_SndOn;
        }
        else {
            nPauseMenu[0] = nSettingsMenu[0] = StringsID.nStrID_SndOff;
            MTMCanvas.soundStop(MTMCanvas.soundId);
        }
    }
    //#endif

    /**
     *
     */
    //#if USE_GAME_TIPS
    public static boolean isGameTipsOn() {
        //#if SOUND_NOKIA_RT || SOUND_MMAPI || SAMSUNG_MMF || SHARP_MMF
            //#if USE_SOUND_VOLUME_CONTROL
                int idx = 2;
            //#else
                int idx = 1;
            //#endif
        //#else
            int idx = 0;
        //#endif
        return (nSettingsMenu[idx] == StringsID.nStrID_GameTipsOn);
    }

    /**
     *
     */
    public static void enableGameTips(boolean on)
    {
        //#if SOUND_NOKIA_RT || SOUND_MMAPI || SAMSUNG_MMF || SHARP_MMF
            //#if USE_SOUND_VOLUME_CONTROL
                int idxS = 2;
                int idxP = 1;
            //#else
                int idxS = 1;
                int idxP = 1;
            //#endif
        //#else
            int idxS = 0;
            int idxP = 0;
        //#endif
        if (on) {
            nPauseMenu[idxP] = nSettingsMenu[idxS] = StringsID.nStrID_GameTipsOn;
        }
        else {
            nPauseMenu[idxP] = nSettingsMenu[idxS] = StringsID.nStrID_GameTipsOff;
        }
    }
    //#endif
   
    /**
     *
     */
    public static void Init(MTMCanvas canv) {
        nCurMenu = null;
        blNeedRedraw = true;
        iMenuOption = 0;
        blNeedRecalcSize = true;
        iMenuWidth = 0;
        iMenuHeight = 0;
        iMenuScreenID = MENU_SCR_NONE;

        //#if !FULL_CANVAS
            canvas = canv;
        //#endif

        if (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_EDIT_NAME) {
            initEditor();
            setMenu(MENU_ID_EDIT_NAME, false);
        }
        //#if SOUND_NOKIA_RT || SOUND_MMAPI || SAMSUNG_MMF || SHARP_MMF
            else if (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_ENABLE_SOUND) {
                strToText(StringsID.nStrID_EnableSound, true);
                setMenu(MENU_ID_QUESTION, false);
            }
        //#endif
        else if (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_MENU) {
            splashLoad(false);
            setMenu(MENU_ID_MAIN, false);
        }
        else if (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_LOADING) {
            strToText(StringsID.nStrID_Loading, true);
        }
        //#if USE_GAME_TIPS
            else if (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_SHOW_TIP) {
                int th = 2 * MTMCanvas.height / 5;
                nMaxWndStr = th / Resources.stringHeight(Resources.nOptFlg_NormalFnt);
                setMenu(MENU_ID_GAME_TIPS, false);
            }
        //#endif
        else if (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_IN_GAME_MENU) {
            //#if PAUSE_MENU_BG_SPLASH
                splashLoad(false);
            //#endif
            setMenu(MENU_ID_PAUSE, false);
        }
        else if (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_FINAL_SCENE) {
            strToText(StringsID.nStrID_FinalText, false);
            //#if USE_FINAL_SPLASH
                imgFinalScene = Resources.LoadImage("/res/finalscene");
                nMaxWndStr = (MTMCanvas.height - imgFinalScene.getHeight() - MENU_BAR_HEIGHT) / Resources.stringHeight(Resources.nOptFlg_NormalFnt);
            //#else
                nMaxWndStr = (MTMCanvas.height - MENU_BAR_HEIGHT - 5) / Resources.stringHeight(Resources.nOptFlg_NormalFnt);
            //#endif
            setMenu(MENU_ID_FINAL_SCENE, false);
        }
        //#if !SCORE_SAVE_RMS
            else if (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_UPLOAD_SCORE) {
                strToText(StringsID.nStrID_UploadScore, true);
                setMenu(MENU_ID_QUESTION, false);
            }
        //#endif
        else if (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_SPLASH) {
            //#if !FULL_CANVAS
                if (canvas != null) {
                    canvas.removeAllCmds();
                }
            //#endif
            splashLoad(true);
            nCounter = 40;
        }
        blReady = true;
    }

    /**
     *
     */
    public static void splashLoad(boolean full) {
        //#if !(SCREENGROUP_128x127xSpecial || SCREENGROUP_128x128xSpecial || SCREENGROUP_128x160xSpecial)
            imgSplash = Resources.LoadImage("/res/splash");
        //#endif
        imgTitle = Resources.LoadImage("/res/title");
        if (full) {
            //#if !(SCREENGROUP_128x127xSpecial || SCREENGROUP_128x128xSpecial || SCREENGROUP_128x160xSpecial)
                imgMarv = Resources.LoadImage("/res/marv");
            //#endif
            imgCopyright = Resources.LoadImage("/res/copyright");
        }
    }

    /**
     *
     */
    public static void strToText(int messId, boolean border) {
        vText.removeAllElements();
        if (border) {
            Resources.stringFragmentation(vText, Resources.GetString(messId), 0, MTMCanvas.width-(TileDrawer.nTileWidth << 1)-10);
        }
        else {
            Resources.stringFragmentation(vText, Resources.GetString(messId), 0, MTMCanvas.width-10);
        }
        textSize = vText.size();
        nCurLine = 0;
        nMaxWndStr = (MTMCanvas.height - messPosY - MENU_BAR_HEIGHT) / Resources.stringHeight(Resources.nOptFlg_NormalFnt);
    }

    /**
     *
     */
    private static void setMenu(byte menuId, boolean save) {
        //#if !FULL_CANVAS
            if (canvas != null) {
                canvas.removeAllCmds();
            }
        //#endif
        if (save) {
            //#if (SCREENGROUP_128x128xSpecial || SCREENGROUP_128x160xSpecial)
                vMenuStack.addElement(new Byte(curMenuId));
            //#else
                vMenuStack.addElement(new MenuSaver(curMenuId, iMenuOption));
            //#endif
        }

        curMenuId = menuId;
        iMenuOption = 0;
        blNeedRecalcSize = true;

        switch (menuId) {
            case MENU_ID_EDIT_NAME:
                nCurMenu = null;
                mLeftBtn = StringsID.nStrID_Ok;
                mRightBtn = StringsID.nStrID_Clear;

                iMenuScreenID = MENU_SCR_EDIT_NAME;
            break;

            case MENU_ID_QUESTION:
                nCurMenu = null;
                mLeftBtn = StringsID.nStrID_Yes;
                mRightBtn = StringsID.nStrID_No;

                iMenuScreenID = MENU_SCR_MESSAGE_BOX;
            break;

            case MENU_ID_INFORMATION:
                nCurMenu = null;
                mLeftBtn = -1;
                mRightBtn = StringsID.nStrID_Back;

                iMenuScreenID = MENU_SCR_MESSAGE_BOX;
            break;

            case MENU_ID_MAIN:
                nCurMenu = nMainMenu;
                mLeftBtn = StringsID.nStrID_Select;
                mRightBtn = StringsID.nStrID_Quit;

                iMenuScreenID = MENU_SCR_NONE;
            break;

            case MENU_ID_PAUSE:
                nCurMenu = nPauseMenu;
                mLeftBtn = StringsID.nStrID_Select;
                mRightBtn = StringsID.nStrID_Resume;

                iMenuScreenID = MENU_SCR_NONE;
            break;

            case MENU_ID_HIGH_SCORE:
                nCurMenu = null;
                mLeftBtn = -1;
                mRightBtn = StringsID.nStrID_Back;

                iMenuScreenID = MENU_SCR_HIGH_SCORE;
            break;

            case MENU_ID_SELECT_LEVEL:
                nCurMenu = null;
                mLeftBtn = StringsID.nStrID_Select;
                mRightBtn = StringsID.nStrID_Back;

                iMenuScreenID = MENU_SCR_SELECT_LEV;
            break;

            case MENU_ID_SETTINGS:
                nCurMenu = nSettingsMenu;
                mLeftBtn = StringsID.nStrID_Select;
                mRightBtn = StringsID.nStrID_Back;

                iMenuScreenID = MENU_SCR_NONE;
            break;

            case MENU_ID_INFO:
                nCurMenu = nInfoMenu;
                mLeftBtn = StringsID.nStrID_Select;
                mRightBtn = StringsID.nStrID_Back;

                iMenuScreenID = MENU_SCR_NONE;
            break;

            case MENU_ID_HELP:
            case MENU_ID_VIEW_CTRLS:
            case MENU_ID_ABOUT:
                nCurMenu = null;
                mLeftBtn = -1;
                mRightBtn = StringsID.nStrID_Back;

                iMenuScreenID = MENU_SCR_HELP;
            break;

            case MENU_ID_FINAL_SCENE:
                nCurMenu = null;
                mLeftBtn = StringsID.nStrID_Ok;
                mRightBtn = -1;

                iMenuScreenID = MENU_SCR_FINAL_SCENE;
            break;

            //#if USE_SOUND_VOLUME_CONTROL
                case MENU_ID_VOLUME_CONTROL:
                    nCurMenu = null;
                    mLeftBtn = StringsID.nStrID_Ok;
                    mRightBtn = -1;

                    iMenuScreenID = MENU_SCR_VOLUME_CONTROL;
                break;
            //#endif

            //#if USE_GAME_TIPS
                case MENU_ID_GAME_TIPS:
                    nCurMenu = null;
                    mLeftBtn = -1;
                    mRightBtn = StringsID.nStrID_Ok;

                    iMenuScreenID = MENU_SCR_GAME_TIPS;
                break;
            //#endif
        }

        //#if !FULL_CANVAS
            if (canvas != null) {
                canvas.removeAllCmds();
                canvas.addCmd(mLeftBtn, mRightBtn);
            }
        //#endif
    }

    /**
     *
     */
    private static void restoreMenu() {
        int size = vMenuStack.size();
        if (size != 0) {
            //#if (SCREENGROUP_128x128xSpecial || SCREENGROUP_128x160xSpecial)
                Byte ms = (Byte)vMenuStack.elementAt(size-1);

                setMenu(ms.byteValue(), false);
                iMenuOption = 0;
            //#else
                MenuSaver ms = (MenuSaver)vMenuStack.elementAt(size-1);

                setMenu(ms.menuId, false);
                iMenuOption = ms.menuOption;
            //#endif
            vMenuStack.removeElementAt(size-1);
        }
    }

    /**
     *
     */
    public static void CleanUp() {
        iMenuScreenID = -1;

        vMenuStack.removeAllElements();
        vText.removeAllElements();
      
        //#if USE_FINAL_SPLASH
            imgFinalScene = null;
        //#endif

        imgSplash = null;
        imgMarv = null;
        imgTitle = null;
        imgCopyright = null;

        System.gc();
    }

    /**
     *
     */
    public static boolean tick() {
        return (--nCounter == 0);
    }

    /**
     *
     */
    private static void initEditor() {
        /* table-version
        editorPosX = 0;
        editorPosY = 0;
        */
        lastKeyNum = -1;
        lastLetter = 0;
        lastTime = 0;
        //#if Nokia && E61
            MTMCanvas.keyCode = 0;
        //#endif

        GameMenu.editStr.setLength(EDITOR_MAX_LENGTH);
        for (int i=editorLength; i<EDITOR_MAX_LENGTH; i++) {
            editStr.setCharAt(i, ' ');
        }
    }

    /**
     *
     */
    //#if Nokia && E61
        private static void processEditorKey(int keyNum) {
            if (editStr != null && editorLength < EDITOR_MAX_LENGTH && keyNum >= 0 && keyNum < NUM_TO_LETTER.length) {
                editStr.setCharAt(editorLength, NUM_TO_LETTER[keyNum]);
                editorLength++;
            }
        }
    //#else
        private static void processEditorKey(int keyNum) {
            long curTime = System.currentTimeMillis();
            if (keyNum == lastKeyNum) {
                if ((curTime - lastTime) < EDITOR_DELAY) {
                    lastLetter++;
                    if (lastLetter >= NUM_TO_LETTER[lastKeyNum].length) {
                    lastLetter = 0;
                    }
                    editorLength--;
                }
                else {
                    lastLetter = 0;
                }
            }
            else {
                lastKeyNum = keyNum;
                lastLetter = 0;
            }
            if (editorLength < EDITOR_MAX_LENGTH) {
                editStr.setCharAt(editorLength, NUM_TO_LETTER[lastKeyNum][lastLetter]);
                editorLength++;
                lastTime = curTime;
            }
            else {
                lastTime = 0;
            }
        }
    //#endif

    /**
     *
     */
    public static int ProcessKeys() {
        int ret = MENU_RET_CMD_NONE;

        switch (iMenuScreenID) {
            case MENU_SCR_HELP:
            case MENU_SCR_HIGH_SCORE:
            case MENU_SCR_FINAL_SCENE:
                if (vText != null) {
                    if (MTMCanvas.keyWasPressed(Constants.keyDOWN | Constants.keyNUM8)) {
                        if ((nCurLine + nMaxWndStr) < textSize)
                            nCurLine++;
                    }
                    else if (MTMCanvas.keyWasPressed(Constants.keyUP | Constants.keyNUM2)) {
                        if (nCurLine > 0) nCurLine--;
                    }
                    else if (MTMCanvas.keyWasPressed(Constants.keyLEFTSOFT | Constants.keyFIRE | Constants.keyNUM5)) {
                        return mLeftBtn;
                    }
                    else if (MTMCanvas.keyWasPressed(Constants.keyRIGHTSOFT)) {
                        restoreMenu();
                    }

                    blNeedRedraw = true;
                    return MENU_RET_CMD_NONE;
                }
            break;

            //#if USE_GAME_TIPS
                case MENU_SCR_GAME_TIPS:
                    if (vText != null) {
                        if (MTMCanvas.keyWasPressed(Constants.keyDOWN | Constants.keyNUM8)) {
                            if ((nCurLine + nMaxWndStr) < textSize) nCurLine++;
                        }
                        else if (MTMCanvas.keyWasPressed(Constants.keyUP | Constants.keyNUM2)) {
                            if(nCurLine > 0) nCurLine--;
                        }
                        else if (MTMCanvas.keyWasPressed(Constants.keyRIGHTSOFT | Constants.keyFIRE | Constants.keyNUM5)) {
                            return mRightBtn;
                        }
                        blNeedRedraw = true;
                        return MENU_RET_CMD_NONE;
                    }
                break;
            //#endif

            case MENU_SCR_SELECT_LEV: {
                // scrolling of menu items
                if (MTMCanvas.keyWasPressed(Constants.keyDOWN | Constants.keyNUM8)) {
                    nSelLine++;
                    if (nSelLine > Hero.m_nLevelMax) nSelLine = Hero.m_nLevelMax;
                    if ((nCurLine + nMaxWndStr) <= nSelLine) nCurLine = nSelLine - nMaxWndStr + 1;
                }
                else if (MTMCanvas.keyWasPressed(Constants.keyUP | Constants.keyNUM2)) {
                    nSelLine--;
                    if (nSelLine < 0) nSelLine = 0;
                    if(nSelLine < nCurLine) nCurLine = nSelLine;
                }
                //menu cancelling
                else if (MTMCanvas.keyWasPressed(Constants.keyRIGHTSOFT)) {
                    restoreMenu();
                    return MENU_RET_CMD_NONE;
                }
                //selecting of menu item
                else if (MTMCanvas.keyWasPressed(Constants.keyLEFTSOFT | Constants.keyFIRE | Constants.keyNUM5)) {
                    return StringsID.nStrID_SelectLevel;
                }
                if (nCurLine < 0) nCurLine = 0;
                blNeedRedraw = true;
            }
            break;

            case MENU_SCR_EDIT_NAME: {
                // symbol inputing
                //#if Nokia && E61
                    if (MTMCanvas.keyCode == Constants.pdKeyQ) {
                        processEditorKey(0);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyW) {
                        processEditorKey(1);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyE) {
                        processEditorKey(2);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKey1 || MTMCanvas.keyCode == Constants.pdKeyR) {
                        processEditorKey(3);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKey2 || MTMCanvas.keyCode == Constants.pdKeyT) {
                        processEditorKey(4);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKey3 || MTMCanvas.keyCode == Constants.pdKeyY) {
                        processEditorKey(5);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeySTAR || MTMCanvas.keyCode == Constants.pdKeyU) {
                        processEditorKey(6);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyI) {
                        processEditorKey(7);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyO) {
                        processEditorKey(8);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyP) {
                        processEditorKey(9);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyA) {
                        processEditorKey(10);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyS) {
                        processEditorKey(11);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyD) {
                        processEditorKey(12);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKey4 || MTMCanvas.keyCode == Constants.pdKeyF) {
                        processEditorKey(13);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKey5 || MTMCanvas.keyCode == Constants.pdKeyG) {
                        processEditorKey(14);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKey6 || MTMCanvas.keyCode == Constants.pdKeyH) {
                        processEditorKey(15);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyPOUND || MTMCanvas.keyCode == Constants.pdKeyJ) {
                        processEditorKey(16);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyK) {
                        processEditorKey(17);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyL) {
                        processEditorKey(18);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyZ) {
                        processEditorKey(19);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyX) {
                        processEditorKey(20);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyC) {
                        processEditorKey(21);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKey7 || MTMCanvas.keyCode == Constants.pdKeyV) {
                        processEditorKey(22);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKey8 || MTMCanvas.keyCode == Constants.pdKeyB) {
                        processEditorKey(23);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKey9 || MTMCanvas.keyCode == Constants.pdKeyN) {
                        processEditorKey(24);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKey0 || MTMCanvas.keyCode == Constants.pdKeyM) {
                        processEditorKey(25);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyDOT) {
                        processEditorKey(26);
                    }
                    else if (MTMCanvas.keyCode == Constants.pdKeyCOMMA) {
                        processEditorKey(27);
                    }
                //#else
                    if (MTMCanvas.keyWasPressed(Constants.keyNUM1)) {
                        processEditorKey(0);
                    }
                    else if (MTMCanvas.keyWasPressed(Constants.keyNUM2)) {
                        processEditorKey(1);
                    }
                    else if (MTMCanvas.keyWasPressed(Constants.keyNUM3)) {
                        processEditorKey(2);
                    }
                    else if (MTMCanvas.keyWasPressed(Constants.keyNUM4)) {
                        processEditorKey(3);
                    }
                    else if (MTMCanvas.keyWasPressed(Constants.keyNUM5)) {
                        processEditorKey(4);
                    }
                    else if (MTMCanvas.keyWasPressed(Constants.keyNUM6)) {
                        processEditorKey(5);
                    }
                    else if (MTMCanvas.keyWasPressed(Constants.keyNUM7)) {
                        processEditorKey(6);
                    }
                    else if (MTMCanvas.keyWasPressed(Constants.keyNUM8)) {
                        processEditorKey(7);
                    }
                    else if (MTMCanvas.keyWasPressed(Constants.keyNUM9)) {
                        processEditorKey(8);
                    }
                //#endif
                //saving
                else  if (MTMCanvas.keyWasPressed(Constants.keyLEFTSOFT | Constants.keyFIRE)) {
                    if (editorLength == 0) {
                        strToText(StringsID.nStrID_YouMustEnterName, true);
                        setMenu(MENU_ID_INFORMATION, true);
                    }
                    else
                    {
                        restoreMenu();
                        Resources.saveSettings();
                        return StringsID.nStrID_Ok;
                    }
                }
                //clearing
                else  if (MTMCanvas.keyWasPressed(Constants.keyRIGHTSOFT)) {
                    lastTime = 0;
                    lastLetter = 0;

                    editorLength--;
                    if (editorLength < 0 ) {
                        editorLength = 0;
                    }
                    editStr.setCharAt(editorLength, ' ');
                }

                blNeedRedraw = true;
                //#if Nokia && E61
                    MTMCanvas.keyCode = 0;
                //#endif

                /* table-version
                if (MTMCanvas.keyWasPressed(Constants.keyLEFT | Constants.keyNUM4)) {
                    editorPosX--;
                    if (editorPosX < 0) {
                        editorPosX = TABLE_OF_LETTER[0].length - 1;
                    }
                }
                else if (MTMCanvas.keyWasPressed(Constants.keyRIGHT | Constants.keyNUM6)) {
                    editorPosX++;
                    if (editorPosX == TABLE_OF_LETTER[0].length) {
                        editorPosX = 0;
                    }
                }
                else if (MTMCanvas.keyWasPressed(Constants.keyUP | Constants.keyNUM2)) {
                    editorPosY--;
                    if (editorPosY < 0) {
                        editorPosY = TABLE_OF_LETTER.length - 1;
                    }
                }
                else if (MTMCanvas.keyWasPressed(Constants.keyDOWN | Constants.keyNUM8)) {
                    editorPosY++;
                    if (editorPosY == TABLE_OF_LETTER.length) {
                        editorPosY = 0;
                    }
                }
                //entering symbol
                else if (MTMCanvas.keyWasPressed(Constants.keyLEFTSOFT | Constants.keyFIRE | Constants.keyNUM5)) {
                    boolean flag = true; // indicate which type of symbol selected: true - simple symbol
                    // special symbol
                    if (editorPosY == (TABLE_OF_LETTER.length - 1)) {
                        //clearing
                        if (editorPosX == (TABLE_OF_LETTER[0].length - 2)) {
                            flag = false;

                            editorLength--;
                            if (editorLength < 0 ) {
                                editorLength = 0;
                            }
                            editStr.setCharAt(editorLength, ' ');
                        }
                        // saving
                        else if(editorPosX == (TABLE_OF_LETTER[0].length - 1)) {
                            flag = false;

                            if (editorLength == 0) {
                                strToText(StringsID.nStrID_YouMustEnterName, true);
                                setMenu(MENU_ID_INFORMATION, true);
                            }
                            else {
                                restoreMenu();
                                Resources.saveSettings();
                                return StringsID.nStrID_Ok;
                            }
                        }
                    }
                    // simple symbol
                    if (flag && (editorLength < EDITOR_MAX_LENGTH)) {
                        editStr.setCharAt(editorLength, TABLE_OF_LETTER[editorPosY][editorPosX].charAt(0));
                        editorLength++;
                    }
                }

                blNeedRedraw = true;
                */
            }
            break;

            case MENU_SCR_MESSAGE_BOX: {
                //left button selected
                if (MTMCanvas.keyWasPressed(Constants.keyLEFTSOFT | Constants.keyFIRE | Constants.keyNUM5)) {
                    if (mLeftBtn != -1) {
                        return mLeftBtn;
                    }
                }
                //right button selected
                else if (MTMCanvas.keyWasPressed(Constants.keyRIGHTSOFT)) {
                    if (mRightBtn != -1) {
                        if (mRightBtn == StringsID.nStrID_Back) {
                            restoreMenu();
                        }
                        else {
                            return mRightBtn;
                        }
                    }
                }

                blNeedRedraw = true;
            }
            break;

            //#if USE_SOUND_VOLUME_CONTROL
                case MENU_SCR_VOLUME_CONTROL: {
                    //left button selected
                    if (MTMCanvas.keyWasPressed(Constants.keyLEFTSOFT | Constants.keyFIRE | Constants.keyNUM5)) {
                        restoreMenu();
                        Resources.saveSettings();
                        MTMCanvas.soundStop(MTMCanvas.soundId);
                    }
                    //change volume
                    else if (MTMCanvas.keyWasPressed(Constants.keyLEFT | Constants.keyNUM4)) {
                        MTMCanvas.volumeLevel -= 5;
                        if (MTMCanvas.volumeLevel < 0) {
                            MTMCanvas.volumeLevel = 0;
                        }

                        for (int i = 0; i < MTMCanvas.volumeControl.length; i++) {
                            MTMCanvas.volumeControl[i].setLevel(MTMCanvas.volumeLevel);
                        }
                    }
                    else if (MTMCanvas.keyWasPressed(Constants.keyRIGHT | Constants.keyNUM6)) {
                        MTMCanvas.volumeLevel += 5;
                        if (MTMCanvas.volumeLevel > 100) {
                            MTMCanvas.volumeLevel = 100;
                        }

                        for (int i = 0; i < MTMCanvas.volumeControl.length; i++) {
                            MTMCanvas.volumeControl[i].setLevel(MTMCanvas.volumeLevel);
                        }
                    }
                    blNeedRedraw = true;
                }
                break;
            //#endif

            default: {
                if (nCurMenu == null) return MENU_RET_CMD_NONE;

                //menu navigation
                if (MTMCanvas.keyWasPressed(Constants.keyDOWN | Constants.keyNUM8)) {
                    if (++iMenuOption >= nCurMenu.length) iMenuOption = nCurMenu.length - 1;
                }
                else if (MTMCanvas.keyWasPressed(Constants.keyUP | Constants.keyNUM2)) {
                    if (--iMenuOption < 0) iMenuOption = 0;
                }
                //menu canceling
                else if (MTMCanvas.keyWasPressed(Constants.keyRIGHTSOFT)) {
                    if (mRightBtn == StringsID.nStrID_Resume) {
                        return StringsID.nStrID_Resume;
                    }
                    else if (mRightBtn == StringsID.nStrID_Quit) {
                        return StringsID.nStrID_Quit;
                    }

                    restoreMenu();
                    return MENU_RET_CMD_NONE;
                }
                //menu item selecting
                else if (MTMCanvas.keyWasPressed(Constants.keyLEFTSOFT | Constants.keyFIRE | Constants.keyNUM5)) {
                    ret = nCurMenu[iMenuOption];
                    switch (nCurMenu[iMenuOption]) {
                        //#if SOUND_NOKIA_RT || SOUND_MMAPI || SAMSUNG_MMF || SHARP_MMF
                            case StringsID.nStrID_SndOff:
                            case StringsID.nStrID_SndOn:
                                enableSound(!isSoundOn());
                                blNeedRecalcSize = true;
                            break;
                        //#endif

                        //#if USE_SOUND_VOLUME_CONTROL
                            case StringsID.nStrID_Volume:
                                MTMCanvas.soundPlay(Constants.SOUND_THEME);
                                setMenu(MENU_ID_VOLUME_CONTROL, true);
                            break;
                        //#endif
                
                        //#if USE_GAME_TIPS
                            case StringsID.nStrID_GameTipsOff:
                            case StringsID.nStrID_GameTipsOn:
                                enableGameTips(!isGameTipsOn());
                                blNeedRecalcSize = true;
                            break;
                        //#endif
                  
                        case StringsID.nStrID_Settings:
                            setMenu(MENU_ID_SETTINGS, true);
                        break;

                        case StringsID.nStrID_HighScores:
                            Resources.loadHighScores(vText);
                            textSize = vText.size() >> 1;

                            nCurLine = 0;
                            nSelLine = 0;

                            nMaxWndStr = (MTMCanvas.height - messPosY - MENU_BAR_HEIGHT) / Resources.stringHeight(Resources.nOptFlg_NormalFnt);

                            setMenu(MENU_ID_HIGH_SCORE, true);
                        break;

                        case StringsID.nStrID_SelectLevel:
                            nCurLine = 0;
                            nSelLine = 0;

                            //#if IMGGROUP_12x12 || IMGGROUP_12x12xLimited
                                int h = Resources.stringHeight(Resources.nOptFlg_NormalFnt);
                            //#else
                                int h = 2 + Resources.stringHeight(Resources.nOptFlg_NormalFnt);
                            //#endif

                            int offset = (MTMCanvas.height >> 1) + animSplash.getLayerProperty(SPLASH_TITLE_LAYER_INDEX, Constants.LAYERPROPERTY_OFFSET_Y);
                            //#if IMGGROUP_12x12 || IMGGROUP_12x12xLimited
                                int nh = MTMCanvas.height - PicDrawer.GetPicHeight(PicID.nPicID_title) - MENU_BAR_HEIGHT - offset - 10;
                            //#elif IMGGROUP_18x18
                                int nh = MTMCanvas.height - PicDrawer.GetPicHeight(PicID.nPicID_title) - MENU_BAR_HEIGHT - offset - 15;
                            //#elif IMGGROUP_24x24 || IMGGROUP_24x24xLimited
                                int nh = MTMCanvas.height - PicDrawer.GetPicHeight(PicID.nPicID_title) - MENU_BAR_HEIGHT - offset - 20;
                            //#elif IMGGROUP_36x36
                                int nh = MTMCanvas.height - PicDrawer.GetPicHeight(PicID.nPicID_title) - MENU_BAR_HEIGHT - offset - 30;
                            //#endif

                            nMaxWndStr = nh / h;

                            setMenu(MENU_ID_SELECT_LEVEL, true);
                            return MENU_RET_CMD_NONE;

                        case StringsID.nStrID_EditName:
                            initEditor();
                            setMenu(MENU_ID_EDIT_NAME, true);
                        break;

                        case StringsID.nStrID_Help:
                            setMenu(MENU_ID_INFO, true);
                        break;

                        case StringsID.nStrID_HowToPlay:
                            strToText(StringsID.nStrID_HelpText, true);
                            setMenu(MENU_ID_HELP, true);
                        break;

                        case StringsID.nStrID_Controls:
                            strToText(StringsID.nStrID_ControlsText, true);
                            setMenu(MENU_ID_VIEW_CTRLS, true);
                        break;

                        case StringsID.nStrID_About:
                            strToText(StringsID.nStrID_AboutText, true);
                            setMenu(MENU_ID_ABOUT, true);
                        break;
                    }
                }

                blNeedRedraw = true;
            }
            break;
        }
        return ret;
    }

    /**
     *
     */
    private static void RecalcCurMenuSize() {
        if (blReady && (nCurMenu != null)) {
            if (blNeedRecalcSize) {
                iMenuWidth = iMenuHeight = 0;

                //#if IMGGROUP_12x12 || IMGGROUP_12x12xLimited
                    int h = Resources.stringHeight(Resources.nOptFlg_NormalFnt);
                //#else
                    int h = 2 + Resources.stringHeight(Resources.nOptFlg_NormalFnt);
                //#endif

                int len = nCurMenu.length;
                int maxPossibleWidth;
                //#if Nokia && E61
                    maxPossibleWidth = maxPossibleWidth = MTMCanvas.width;
                //#else
                    maxPossibleWidth = MTMCanvas.width - (MTMCanvas.width >> 3); 
                //#endif
                for (int i = 0; i < len; i++) {
                    iMenuHeight += h;
                    int w = Resources.stringWidth( nCurMenu[i], Resources.nOptFlg_NormalFnt );
                    if (w > maxPossibleWidth) {
                        int maxW = 0;
                        int vTextSize;
                        vText.removeAllElements();
                        Resources.stringFragmentation(vText, Resources.GetString(nCurMenu[i]), 0, maxPossibleWidth);
                        vTextSize = vText.size();
                        for (int z=0; z < vTextSize; z++) {
                            w = Resources.stringWidth((String)vText.elementAt(z), Resources.nOptFlg_NormalFnt);
                            if (w > maxW) maxW = w;
                        }
                        w = maxW;
                    }
                    if( iMenuWidth < w ) iMenuWidth = w;
                }

                //#if IMGGROUP_12x12 || IMGGROUP_12x12xLimited
                    iMenuHeight += 2;
                //#else
                    iMenuHeight += 4;
                //#endif
                iMenuWidth += 8;
                if (iMenuWidth > MTMCanvas.width) iMenuWidth = MTMCanvas.width;

                blNeedRecalcSize = false;
            }
        }
        else iMenuWidth = iMenuHeight = 0;
    }

   /**
   * @param items  amount of scrolled items
   * @param pos  amount of items that can be placed at scroll window
   * @param left  scroll window's left position
   * @param top  scroll window's top position
   * @param right  scrollbar's position 
   * @param w  scrollbar's width
   * @param h  scrollbar's height
   * @param border  if is true than draw border
   */
    private static void DrawScrolledArea( Graphics g, int items, int pos, int left, int top, int right, int w, int h, boolean border) {
        // scroller
        if (nMaxWndStr < items) {
            int n_scroll_bar_height = (h * nMaxWndStr) / items;
            int d = (items - pos);
            int n_scroll_bar_pos = (d != 0) ? ( ( (h - n_scroll_bar_height) * nCurLine) / d) : 0;

            MTMCanvas.fillRect(g, right - w, top, w, h, 0x773803);
            MTMCanvas.fillRect(g, right - w, top + n_scroll_bar_pos, w, n_scroll_bar_height, 0xE9D11B);
        }

        if (border) {
            MTMCanvas.drawRect(g, left, top, right - left, h, 0xffffff);
        }
    }

    /**
     *
     */
   private static void drawMessageBox( Graphics g ) {
        int i, j;
        int xx, yy;

        // set inital position for draw background
        int x = (MTMCanvas.width - (bTilesMatrix.length * TileDrawer.nTileWidth)) >> 1;
        int y = (MTMCanvas.height - (bTilesMatrix[0].length * TileDrawer.nTileWidth)) >> 1;

        // Background
        if ((x > 0) || (y > 0)) {
            MTMCanvas.fillRect(g, 0, 0, MTMCanvas.width, MTMCanvas.height, 0x000000);
        }
        g.setClip(0, 0, MTMCanvas.width, MTMCanvas.height);

        xx = x;
        for (i = 0; i < bTilesMatrix.length; i++, xx += TileDrawer.nTileWidth) {
            yy = y;
            for (j = 0; j < bTilesMatrix[0].length; j++, yy += TileDrawer.nTileWidth) {
                TileDrawer.DrawTile(g, xx, yy, bTilesMatrix[i][j]);
            }
        }

        // Decoration objects
        //#if !IMGGROUP_12x12xLimited
            x = -x;
            y = -y;
            if (decorObjects != null) {
                for (i = 0; i < decorObjects.length; i++) {
                    decorObjects[i].Draw(g, x, y);
                    decorObjects[i].animationCurrent.Tick();
                }
            }
        //#endif
    }

    /**
     *
     */
    public static int drawText( Graphics g, int x, int y, int anchor ) {
        int n_len = 1;
        int n_cnt = 0;

        if (vText != null) {
            n_len = textSize;
            n_cnt = n_len < nMaxWndStr ? n_len : nMaxWndStr;
            int nEndLine = nCurLine + n_cnt;
            int fh = Resources.stringHeight(Resources.nOptFlg_NormalFnt);

            int str_anchor = Graphics.TOP;

            if ((anchor & ( Graphics.HCENTER ) ) == Graphics.HCENTER) {
                str_anchor |= Graphics.HCENTER;
            }

            if ( ( anchor & ( Graphics.LEFT ) ) == Graphics.LEFT ) {
                str_anchor |= Graphics.LEFT;
            }

            if ( ( anchor & ( Graphics.RIGHT ) ) == Graphics.RIGHT ) {
                str_anchor |= Graphics.RIGHT;
            }

            if ( ( anchor & ( Graphics.BOTTOM ) ) == Graphics.BOTTOM ) {
                y -= fh * n_cnt;
            }

            if ( ( anchor & ( Graphics.VCENTER ) ) == Graphics.VCENTER ) {
                y -= (fh * n_cnt) >> 1;
            }
         
            for (int i = nCurLine; i < nEndLine && i < vText.size(); i++) {
                Resources.DrawString(g, (String)vText.elementAt(i), x, y, str_anchor, Resources.nOptFlg_NormalFnt);
                y += fh;
            }
        }
        return (n_len << 8) | n_cnt;
    }

    /**
     *
     */
    public static void drawSplash(Graphics g) {
        int hw = MTMCanvas.width >> 1;
        int hh = MTMCanvas.height >> 1;
        int shiftX, shiftY;

        g.setClip(0, 0, MTMCanvas.width, MTMCanvas.height);

        //#if IMGGROUP_12x12xLimited || SCREENGROUP_176x204xLimited || IMGGROUP_24x24xLimited
            g.setColor(0x000000);
            g.fillRect(0, 0, MTMCanvas.width, MTMCanvas.height);
        //#endif

        if (imgSplash != null) {
            //splash
            shiftX = animSplash.getLayerProperty(SPLASH_BG_LAYER_INDEX, Constants.LAYERPROPERTY_OFFSET_X);
            shiftY = animSplash.getLayerProperty(SPLASH_BG_LAYER_INDEX, Constants.LAYERPROPERTY_OFFSET_Y);
            g.drawImage(imgSplash, hw + shiftX, hh + shiftY, Graphics.TOP|Graphics.LEFT);
        }

        if (imgTitle != null) {
            //title
            shiftX = animSplash.getLayerProperty(SPLASH_TITLE_LAYER_INDEX, Constants.LAYERPROPERTY_OFFSET_X);
            shiftY = animSplash.getLayerProperty(SPLASH_TITLE_LAYER_INDEX, Constants.LAYERPROPERTY_OFFSET_Y);
            g.drawImage(imgTitle, hw + shiftX, hh + shiftY, Graphics.TOP|Graphics.LEFT);
        }

        //marv
        if (imgMarv != null) {
            shiftX = animSplash.getLayerProperty(SPLASH_MARV_LAYER_INDEX, Constants.LAYERPROPERTY_OFFSET_X);
            shiftY = animSplash.getLayerProperty(SPLASH_MARV_LAYER_INDEX, Constants.LAYERPROPERTY_OFFSET_Y);
            g.drawImage(imgMarv, hw + shiftX, hh + shiftY, Graphics.TOP|Graphics.LEFT);
        }

        // copyright
        if (imgCopyright != null) {
            shiftX = animSplash.getLayerProperty(SPLASH_COPYRIGHT_MARV_INDEX, Constants.LAYERPROPERTY_OFFSET_X);
            shiftY = animSplash.getLayerProperty(SPLASH_COPYRIGHT_MARV_INDEX, Constants.LAYERPROPERTY_OFFSET_Y);
            g.drawImage(imgCopyright, hw + shiftX, hh + shiftY, Graphics.TOP|Graphics.LEFT);
        }
    }

    /**
     *
     */
    public static boolean Draw( Graphics g ) {
        if (!blReady || !blNeedRedraw) return false;

        blNeedRedraw = false;
        RecalcCurMenuSize();

        if ( 
            //#if SOUND_NOKIA_RT || SOUND_MMAPI || SAMSUNG_MMF || SHARP_MMF
                (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_ENABLE_SOUND) ||
            //#endif
            //#if USE_GAME_TIPS
                (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_SHOW_TIP) ||
            //#endif
            (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_MENU) ||
            (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_IN_GAME_MENU) ||
            (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_EDIT_NAME) ||
            (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_FINAL_SCENE)
            //#if !SCORE_SAVE_RMS
                || (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_UPLOAD_SCORE)
            //#endif
           )
        {
            //#if IMGGROUP_12x12xLimited || IMGGROUP_12x12
                //#if USE_SYSTEM_FONT
                    int hNormFnt = Resources.stringHeight(Resources.nOptFlg_NormalFnt) - 1;
                //#else
                    int hNormFnt = Resources.stringHeight(Resources.nOptFlg_NormalFnt);
                //#endif
            //#else
                int hNormFnt = 2 + Resources.stringHeight(Resources.nOptFlg_NormalFnt);
            //#endif

            switch (iMenuScreenID) {
                case MENU_SCR_EDIT_NAME: {
                    drawMessageBox(g);

                    int hBoldFnt = Resources.stringHeight(Resources.nOptFlg_BoldFnt);
                    int y = messPosY + ((MTMCanvas.height - messPosY - MENU_BAR_HEIGHT) >> 1);
                    int x = TileDrawer.nTileWidth << 1;

                    Resources.DrawString(g, StringsID.nStrID_YourName, MTMCanvas.width >> 1, y - hBoldFnt, Graphics.BOTTOM | Graphics.HCENTER, Resources.nOptFlg_NormalFnt);
                    if (MTMCanvas.setClip(g, x, y, MTMCanvas.width - (x << 1) + 1, hBoldFnt + 4, true)) {
                        g.setColor(0xF0C080);
                        g.fillRoundRect(x, y, MTMCanvas.width - (x << 1), hBoldFnt + 3, 2, 2);
                        g.setColor(0xff0000);
                        g.drawRoundRect(x, y, MTMCanvas.width - (x << 1), hBoldFnt + 3, 2, 2);
                    }
                    Resources.DrawString(g, editStr.toString(), x + 4, y + 3, Graphics.TOP | Graphics.LEFT, Resources.nOptFlg_BoldFnt);
                    /* table-version
                    // table of letters
                    int tableWidth = hBoldFnt * TABLE_OF_LETTER[0].length;
                    int tableHeight = hBoldFnt * TABLE_OF_LETTER.length;
                    int tableLeft = (MTMCanvas.width - tableWidth) >> 1;
                    int tableTop = messPosY + 40;

                    y = tableTop;
                    for (int i = 0; i<TABLE_OF_LETTER.length; i++, y += hBoldFnt) {
                        x = tableLeft;
                        for (int j = 0; j<TABLE_OF_LETTER[0].length; j++, x += hBoldFnt) {
                            Resources.DrawString(g, TABLE_OF_LETTER[i][j], x, y, Graphics.TOP | Graphics.LEFT, Resources.nOptFlg_BoldFnt);
                        }
                    }

                    // grid
                    MTMCanvas.setClip(g, tableLeft - 2, tableTop - 2, tableWidth + 1, tableHeight + 1, true);
                    g.setColor(0x777777);
                    g.drawRect(tableLeft - 2, tableTop - 2, tableWidth, tableHeight);

                    y = tableTop + hBoldFnt - 2;
                    for (int i = 1; i<TABLE_OF_LETTER.length; i++, y += hBoldFnt) {
                        g.drawLine(tableLeft - 2, y, tableLeft - 2 + tableWidth, y);
                    }

                    x = tableLeft + hBoldFnt - 2;
                    for (int j = 1; j<TABLE_OF_LETTER[0].length; j++, x += hBoldFnt) {
                        g.drawLine(x, tableTop - 2, x, tableTop - 2 + tableHeight);
                    }

                    // cursor
                    g.setColor(0x00FF00);
                    g.drawRect(tableLeft - 2 + (hBoldFnt * editorPosX), tableTop - 2 + (hBoldFnt * editorPosY), hBoldFnt, hBoldFnt);
                    */
                }
                break;

                case MENU_SCR_SELECT_LEV: {
                    drawSplash(g);

                    int n_cnt = 0;
                    int th = PicDrawer.GetPicHeight(PicID.nPicID_title);
                    int offset = (MTMCanvas.height >> 1) + animSplash.getLayerProperty(SPLASH_TITLE_LAYER_INDEX, Constants.LAYERPROPERTY_OFFSET_Y);

                    //#if IMGGROUP_12x12 || IMGGROUP_12x12xLimited
                        int y = th + offset + 10;
                    //#elif IMGGROUP_18x18
                        int y = th + offset + 15;
                    //#elif IMGGROUP_24x24 || IMGGROUP_24x24xLimited
                        int y = th + offset + 20;
                    //#elif IMGGROUP_36x36
                        int y = th + offset + 30;
                    //#endif

                    int nh = MTMCanvas.height - y - MENU_BAR_HEIGHT;

                    for (int i = nCurLine; (n_cnt < nMaxWndStr) && (n_cnt <= Hero.m_nLevelMax); i++, n_cnt++, y += hNormFnt) {
                        if ( i == nSelLine ) {
                            MTMCanvas.fillRect(g, 9, y-1, MTMCanvas.width - 22, hNormFnt, 0xF0C080);
                        }
                        Resources.DrawString(g, Resources.GetString(StringsID.nStrID_Level) + " " + (i+1), 12, y, Graphics.TOP | Graphics.LEFT, Resources.nOptFlg_NormalFnt);
                    }

                    //#if IMGGROUP_12x12 || IMGGROUP_12x12xLimited
                        DrawScrolledArea( g, 1 + Hero.m_nLevelMax, n_cnt, 5, th+offset+7, MTMCanvas.width - 5, 5, nh, true );
                    //#elif IMGGROUP_18x18
                        DrawScrolledArea( g, 1 + Hero.m_nLevelMax, n_cnt, 5, th+offset+10, MTMCanvas.width - 5, 5, nh, true );
                    //#elif IMGGROUP_24x24 || IMGGROUP_24x24xLimited
                        DrawScrolledArea( g, 1 + Hero.m_nLevelMax, n_cnt, 5, th+offset+13, MTMCanvas.width - 5, 5, nh, true );
                    //#elif IMGGROUP_36x36
                        DrawScrolledArea( g, 1 + Hero.m_nLevelMax, n_cnt, 5, th+offset+20, MTMCanvas.width - 5, 5, nh, true );
                    //#endif
                }
                break;

                case MENU_SCR_HIGH_SCORE: {
                    drawMessageBox(g);

                    int nh = MTMCanvas.height - messPosY - MENU_BAR_HEIGHT;

                    int scroll = drawText(g, TileDrawer.nTileWidth + 3, messPosY + 3, Graphics.TOP | Graphics.LEFT);
                    nCurLine += textSize;
                    drawText(g, MTMCanvas.width - TileDrawer.nTileWidth - 8, messPosY + 3, Graphics.TOP | Graphics.RIGHT);
                    nCurLine -= textSize;

                    if (textSize > nMaxWndStr) {
                        DrawScrolledArea( g, scroll >> 8, scroll & 0xff, TileDrawer.nTileWidth, messPosY, MTMCanvas.width - TileDrawer.nTileWidth, 5, nh, true );
                    }
                }
                break;

                //#if USE_GAME_TIPS
                    case MENU_SCR_GAME_TIPS: {
                        GameWorld.Draw(g);

                        int top;
                        int hh = MTMCanvas.height >> 1;
                        int th = 2 * MTMCanvas.height / 5;

                        if (Hero.m_nPosY > (GameWorld.gps_height - hh)) {
                            // tip on top of screen
                            int mt = GameWorld.gps_height - (Hero.m_nPosY - Hero.animationCurrent.GetCurFrmHeight());
                            top = (MTMCanvas.height - mt - th) >> 1;
                        }
                        else {
                            // tip on bottom of screen
                            if (Hero.m_nPosY > hh) {
                                top = hh + ((hh - th) >> 1);
                            }
                            else {
                                top = Hero.m_nPosY + ((MTMCanvas.height - Hero.m_nPosY - th) >> 1);
                            }
                        }

                        MTMCanvas.fillRect(g, TileDrawer.nTileWidth, top, MTMCanvas.width - (TileDrawer.nTileWidth << 1), th, 0x000000);

                        g.setClip(0, 0, MTMCanvas.width, MTMCanvas.height);
                        g.setColor(0xFFC92F);
                        g.drawLine(TileDrawer.nTileWidth, top - 1, MTMCanvas.width - TileDrawer.nTileWidth - 1, top - 1);
                        g.drawLine(TileDrawer.nTileWidth - 1, top, TileDrawer.nTileWidth - 1, top + th - 1);
                        g.setColor(0xDE8801);
                        g.drawLine(TileDrawer.nTileWidth, top + th, MTMCanvas.width - TileDrawer.nTileWidth - 1, top + th);
                        g.drawLine(MTMCanvas.width - TileDrawer.nTileWidth, top, MTMCanvas.width - TileDrawer.nTileWidth, top + th - 1);
 
                        int scroll = drawText(g, MTMCanvas.width >> 1, top + (th >> 1), Graphics.VCENTER|Graphics.HCENTER);
                        if (textSize > nMaxWndStr) {
                            DrawScrolledArea( g, scroll >> 8, scroll & 0xff, TileDrawer.nTileWidth, top, MTMCanvas.width - TileDrawer.nTileWidth, 5, th, false );
                        }
                    }
                    break;
                //#endif

                case MENU_SCR_HELP: {
                    drawMessageBox(g);

                    int nh = MTMCanvas.height - messPosY - MENU_BAR_HEIGHT;
                    int scroll = drawText(g, TileDrawer.nTileWidth + 3, messPosY + 3, Graphics.TOP | Graphics.LEFT);
                    DrawScrolledArea( g, scroll >> 8, scroll & 0xff, TileDrawer.nTileWidth, messPosY, MTMCanvas.width - TileDrawer.nTileWidth, 5, nh, true );
                }
                break;

                case MENU_SCR_MESSAGE_BOX: {
                    drawMessageBox(g);
                    drawText(g, MTMCanvas.width >> 1, messPosY + ((MTMCanvas.height - messPosY - MENU_BAR_HEIGHT) >> 1), Graphics.VCENTER|Graphics.HCENTER);
                }
                break;

                case MENU_SCR_FINAL_SCENE:
                    //#if USE_FINAL_SPLASH
                    if (imgFinalScene != null) {
                        int th = imgFinalScene.getHeight();
                        g.drawImage(imgFinalScene, MTMCanvas.width >> 1, 0, Graphics.TOP | Graphics.HCENTER);
                        MTMCanvas.fillRect(g, 0, th, MTMCanvas.width, MTMCanvas.height - th, 0x251007);
                    //#else
                    {
                        int th = 5;
                        MTMCanvas.fillRect(g, 0, 0, MTMCanvas.width, MTMCanvas.height, 0x251007);
                    //#endif
                    int nh = MTMCanvas.height - th - MENU_BAR_HEIGHT;
                    int scroll = drawText(g, 3, th + 3, Graphics.TOP|Graphics.LEFT);
                    DrawScrolledArea( g, scroll >> 8, scroll & 0xff, 1, th, MTMCanvas.width - 1, 5, nh, true );
                    }
                break;

                //#if USE_SOUND_VOLUME_CONTROL
                    case MENU_SCR_VOLUME_CONTROL: {
                        drawMessageBox(g);

                        int hBoldFnt = Resources.stringHeight(Resources.nOptFlg_BoldFnt);
                        int y = messPosY + ((MTMCanvas.height - messPosY - MENU_BAR_HEIGHT) >> 1);
                        int x = TileDrawer.nTileWidth << 1;

                        Resources.DrawString(g, StringsID.nStrID_Volume, MTMCanvas.width >> 1, y - hBoldFnt, Graphics.BOTTOM | Graphics.HCENTER, Resources.nOptFlg_NormalFnt);

                        MTMCanvas.fillRect(g, x, y, MTMCanvas.width - (x << 1), 20, 0x4E2B02);
                        MTMCanvas.fillRect(g, x, y, (MTMCanvas.volumeLevel * (MTMCanvas.width - (x << 1))) / 100, 20, 0xF0C080);

                        Resources.DrawString(g, "0", x, y + 25, Graphics.TOP | Graphics.LEFT, Resources.nOptFlg_NormalFnt);
                        Resources.DrawString(g, "100", MTMCanvas.width - x, y + 25, Graphics.TOP | Graphics.RIGHT, Resources.nOptFlg_NormalFnt);
                    }
                    break;
                //#endif

                default: {
                    //#if PAUSE_MENU_BG_RASTER
                        if (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_IN_GAME_MENU) {
                            GameWorld.Draw(g);

                            g.setClip(0, 0, MTMCanvas.width, MTMCanvas.height);
                            for (int x=0; x<MTMCanvas.width; x+=Constants.RASTER_WIDTH)
                                for (int y=0; y<MTMCanvas.height; y+=Constants.RASTER_HEIGHT) {
                                    g.drawImage(imgRastr, x, y, Graphics.TOP|Graphics.LEFT);
                                }
                        }
                        else {
                            drawSplash(g);
                        }
                    //#else
                        drawSplash(g);
                    //#endif

                    // drawing menu items
                    if (nCurMenu != null) {
                        int len = nCurMenu.length;
                        int n_scr_left = 0;
                        int y = 0;

                        //#if PAUSE_MENU_BG_RASTER
                            if (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_IN_GAME_MENU) {
                                n_scr_left = ( (MTMCanvas.width - iMenuWidth) >> 1 );
                                y = (MTMCanvas.height - iMenuHeight) >> 1;
                            }
                            else { 
                                n_scr_left = ( MTMCanvas.width >> 3 );
                                int th = PicDrawer.GetPicHeight(PicID.nPicID_title);
                                y = th + ((MTMCanvas.height - th - iMenuHeight) >> 1);
                            } 
                        //#else
                            //#if Nokia && E61
                                n_scr_left = ((MTMCanvas.width - iMenuWidth) >> 1);
                            //#else
                                n_scr_left = (MTMCanvas.width >> 3);
                            //#endif
                            int th = PicDrawer.GetPicHeight(PicID.nPicID_title);
                            y = th + ((MTMCanvas.height - th - iMenuHeight) >> 1);
                        //#endif
                  
                        int koef = 1;
                        int menuTextSize = 0;
                        for (int i = 0; i < len; i++, y += hNormFnt) {
                            // check width here
                                koef = 1;
                                int sWidth = Resources.stringWidth(nCurMenu[i], Resources.nOptFlg_NormalFnt);
                                if (sWidth > (MTMCanvas.width - n_scr_left)) {
                                    // split string
                                    vText.removeAllElements();
                                    Resources.stringFragmentation(vText, Resources.GetString(nCurMenu[i]), 0, (MTMCanvas.width - n_scr_left));
                                    menuTextSize = vText.size();
                                    koef=menuTextSize;
                                }

                                if (i == iMenuOption) {
                                    //#if IMGGROUP_12x12 || IMGGROUP_12x12xLimited
                                        MTMCanvas.fillRect(g, n_scr_left-4, y-1, iMenuWidth, hNormFnt*koef + 1, 0xF0C080);
                                    //#else
                                        MTMCanvas.fillRect(g, n_scr_left-4, y-1, iMenuWidth, hNormFnt*koef, 0xF0C080);
                                    //#endif
                                }

                                if (koef > 1)
                                    for (int z = 0; z < menuTextSize; z++) {
                                        Resources.DrawString(g, (String)vText.elementAt(z), n_scr_left, y, Graphics.TOP | Graphics.LEFT, Resources.nOptFlg_NormalFnt);
                                        if (z != menuTextSize-1) y += hNormFnt;
                                    }
                                else Resources.DrawString(g, nCurMenu[i], n_scr_left, y, Graphics.TOP | Graphics.LEFT, Resources.nOptFlg_NormalFnt);
                        }
                    }
                }
                break;
            }
            //#if FULL_CANVAS
                // draw additional menu items
                if (mLeftBtn != -1) {
                    //#if IMGGROUP_12x12xLimited || IMGGROUP_12x12
                        Resources.DrawString(g, mLeftBtn, 1, MTMCanvas.height - hNormFnt, Graphics.TOP | Graphics.LEFT, Resources.nOptFlg_NormalFnt);
                    //#else
                        Resources.DrawString(g, mLeftBtn, 7, MTMCanvas.height - hNormFnt, Graphics.TOP | Graphics.LEFT, Resources.nOptFlg_NormalFnt);
                    //#endif
                }

                if (mRightBtn != -1) {
                    //#if IMGGROUP_12x12xLimited || IMGGROUP_12x12
                        Resources.DrawString(g, mRightBtn, MTMCanvas.width - 1, MTMCanvas.height - hNormFnt, Graphics.TOP | Graphics.RIGHT, Resources.nOptFlg_NormalFnt);
                    //#else
                        Resources.DrawString(g, mRightBtn, MTMCanvas.width - 7, MTMCanvas.height - hNormFnt, Graphics.TOP | Graphics.RIGHT, Resources.nOptFlg_NormalFnt);
                    //#endif
                }
            //#endif
        }
        else if (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_SPLASH) {
            drawSplash(g);
        }
        else if (MTMCanvas.nCurrentGameState == MTMCanvas.GAME_STATE_LOADING) {
            //#if !IMGGROUP_12x12xLimited
                drawMessageBox(g);
            //#else
                MTMCanvas.fillRect(g, 0, 0, MTMCanvas.width, MTMCanvas.height, 0x000000);
            //#endif

            drawText(g, MTMCanvas.width >> 1, messPosY + ((MTMCanvas.height - messPosY - MENU_BAR_HEIGHT) >> 1), Graphics.VCENTER|Graphics.HCENTER);

            blNeedRedraw = true;
        }

        return true;
    }
}
